/* eslint-disable no-param-reassign */
import { useStore } from '@/hooks';
import TauTable from '@/components/TauTable';
import UploadPanel from '@/components/UploadPanel';
import { Button, Dialog, Form, ResponsiveGrid, Icon, Input } from '@alifd/next';
import React, { useEffect, useRef, useState } from 'react';
import { t } from '@lingui/macro';
import DrawerFooter from '@/components/DrawerFooter';
import { FieldsGrid, TitleGrid, column, partnerModal } from '../stores/definitions';
import { observer } from 'mobx-react-lite';
import { observable } from 'mobx';

const { Cell } = ResponsiveGrid;

const FromItem = Form.Item;

const DataForm = ({ modal, partnerInfo, onCancel, onSubmit }) => {
  const { partnerStore: store } = useStore();
  // 使用useRef进行拼接字段且不会造成重绘
  const editCtx = useRef({ origin: {}, changed: {} }).current; // 用于计算是否修改
  const [isViewMode] = useState(modal?.visible === 'check');
  const formProps = isViewMode ? { field: false } : {};
  const [partner] = useState(() => observable({}));
  useEffect(() => { if (modal?.close?.startsWith?.('toClose')) handleCancel(); }, [modal]);

  useEffect(() => {
    (async () => {
      if (JSON.stringify(partnerInfo) !== '{}') {
        const data = partnerInfo;
        let initDisable;
        const unDisable = data.partnerInfo?.discountInfo?.disable;
        if (unDisable === undefined) {
          initDisable = true;
        } else {
          initDisable = unDisable;
        }
        editCtx.origin = _.cloneDeep(data);
        _.assign(partner, data);
        store.getCompany({ search: { status: 1 }, pg: { size: 999 } });
        _.set(partner, 'partnerInfo.discountInfo.disable', !initDisable);
      } else {
        _.set(partner, 'partnerInfo.independentAccount.period', 0);
      }
    })();
  }, []);

  /**
   * @description 动态回现表单数据
   * @param {*} values
   * @param {*} item 数据Items
   */
  const handleChange = (values, item) => {
    const { name, value } = item;
    _.set(partner, name, value);
    // eslint-disable-next-line eqeqeq
    editCtx.changed[name] = value == _.get(editCtx.origin, name) ? undefined : value;
  };

  // 重载definitions中的定义，处理一些根据上下文的动态定义
  const overrideModel = {
  };


  /**
   * @description 提交表单实现修改车辆
   * @param {*} r formData
   */
  const handleSubmit = (values, errors) => {
    if (!_.isEmpty(errors)) {
      return;
    }
    onSubmit?.(partner);
  };
  const handleCancel = () => {
    // eslint-disable-next-line no-return-assign
    if (!_.isEmpty(_.transform(editCtx.changed, (r, v, k) => v !== undefined && (r[k] = v)))) {
      Dialog.confirm({
        content: t`放弃修改内容？`,
        onOk: () => onCancel?.(),
      });
      return;
    }
    onCancel?.();
  };

  return (
    <Form
      inline
      isPreview={isViewMode}
      {...formProps}
      value={partner}
      onChange={handleChange}
      useLabelForErrorMessage
    >
      {/* <TitleGrid title={t`基本信息`} /> */}
      {/* <FieldsGrid
        formData={partner}
        mode={modal[0]}
        overrideModel={overrideModel}
        fields={['partnerInfo.type', 'fullname', 'mobile', 'gender', 'email', 'partnerInfo.bindingPeriod', 'partnerInfo.bindingPeriodDesc', 'partnerInfo.address']}
      /> */}
      <h1>查看退款状态</h1>

      <DrawerFooter>
        <FromItem>
          {!isViewMode && <Form.Submit validate type="primary" onClick={handleSubmit}>{t`保存`}</Form.Submit>}
          <Button style={{ marginLeft: 8 }} onClick={handleCancel}>{t`取消`}</Button>
        </FromItem>
      </DrawerFooter>
    </Form>
  );
};

export default observer(DataForm);
